package com.example.d_printstream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class Demo1PrintStream {
    public static void main(String[] args) {
        try(PrintStream ps = new PrintStream("testOs.txt");){
            ps.println("hello world");
            ps.println("hello world12");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

//        设置print的输出流向
        try(PrintStream ps = new PrintStream("testOs.txt");){
            System.setOut(ps);
            System.out.println("gsag");
            System.out.println("235");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }


//        下面两段代码实现的效果一样
        try( PrintStream ps1 = new PrintStream(new FileOutputStream("testOs.txt", true));){
            System.setOut(ps1);
            System.out.println("He says love me");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        try( PrintStream ps1 = new PrintStream(new FileOutputStream("testOs.txt", true));){
            ps1.println("23534");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
